using System.Collections.Generic;
using System.Drawing;
using OhioState.Graphics;
using Tao.OpenGl;

namespace OhioState.Graphics.OpenGL
{
    
    public class ViewGL : IView
    {
        #region Properties
        private Viewport _viewPort = new Viewport();
        public Viewport Viewport
        {
            get { return _viewPort; }
            set { _viewPort = value; }
        }
        //
        // Property for the camera.
        // Note: This camera has little to do with the overall scene camera.
        //   The scene camera is usually held within each layer. This camera
        //   can be used for non-planar projections or off-angle compositing
        //   (although the layers do not have a depth associated with them.
        //
        private ICamera _camera = new Camera();
        public ICamera CurrentCamera
        {
            get { return _camera; }
            set { _camera = value; }
        }
        //private OpenGLPanel openGLPanel;
        #endregion Properties

        public ViewGL()
        {
            
        }

        
        public void AddLayer(ILayer layer)
        {
            if (layer != null)
            {
                _layers.Add(layer);
            }
        }
        public void RemoveLayer(ILayer layer)
        {
            if (layer != null && _layers.Contains(layer))
            {
                _layers.Remove(layer);
            }
        }
        public void RemoveAllLayers()
        {
            _layers.Clear();
        }
      
        private List<ILayer> _layers = new List<ILayer>(20);

        public void CompositeLayers(OhioState.Graphics.IRenderPanel panel)
        {
            //
            // Set-up the viewport
            //
            Gl.glViewport(_viewPort.Origin.x, _viewPort.Origin.y, _viewPort.Width, _viewPort.Height);
            //Gl.glClearColor(0.2f, 0.3f, 0.4f, 1.0f);
            //Gl.glClear(Gl.GL_COLOR);
            //Gl.glDisable(Gl.GL_DEPTH_TEST);
            //
            // Make the camera active - Should I have a Push / Pop around these?
            //
            //Gl.glMatrixMode(Gl.GL_PROJECTION);
            //Gl.glLoadMatrixf(_camera.GetProjectionMatrix().GetStreamColumnOrder());
            //Gl.glMatrixMode(Gl.GL_MODELVIEW);
            //Gl.glLoadMatrixf(_camera.GetModelViewMatrix().GetStreamColumnOrder());
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Gl.glOrtho(0, 1, 0, 1, -10, 100);
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
            
            foreach (ILayer layer in _layers)
            {
                  layer.Render(panel);
            }
        }

        /// <summary>
        /// Render layers as usual, use DevIL to save layer images to the file(s).
        /// The filenames for the layers are filename + layer number (starting from 0) + extension.
        /// </summary>
        public void SaveScreenshot(string filename, string filetype)
        {
            Gl.glViewport(_viewPort.Origin.x, _viewPort.Origin.y, _viewPort.Width, _viewPort.Height);
            //Gl.glDisable(Gl.GL_DEPTH_TEST);
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Gl.glOrtho(0, 1, 0, 1, -10, 100);
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
            

            //int ilFileType = 0;
            //if (filetype.ToLower().Equals("tif"))
            //    ilFileType = Il.IL_TIF;
            //else if (filetype.ToLower().Equals("bmp"))
            //    ilFileType = Il.IL_BMP;
            //else if (filetype.ToLower().Equals("jpg"))
            //    ilFileType = Il.IL_JPG;
            //else if (filetype.ToLower().Equals("png"))
            //    ilFileType = Il.IL_PNG;
            //else if (filetype.ToLower().Equals("tga"))
            //    ilFileType = Il.IL_TGA;

            int currentLayerNumber = 0;
            //int currentLayerName = 0;
            //Il.ilInit();
            //Ilu.iluInit();
            //Ilut.ilutInit();
            //Ilut.ilutRenderer(Ilut.ILUT_OPENGL);
            //Il.ilEnable(Il.IL_FILE_OVERWRITE);//overwrite if the file with the name already exists
            foreach (ILayer layer in _layers)
            {
                //OSUColor backgroundColor = OpenGLPanel.Instance.BackgroundColor;
                //Gl.glClearColor(backgroundColor.R, backgroundColor.G, backgroundColor.B, 1.0f);
                //Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
                //layer.Render();

                //get the real layer (layerHandler)
                LayerHandler realLayer = (LayerHandler)layer;
                Bitmap bitmap = realLayer.copyToBitmap();
                bitmap.Save(filename + currentLayerNumber + ".png", System.Drawing.Imaging.ImageFormat.Png);
                currentLayerNumber++;
                /*
                //clear background before rendering of a next layer, we need separate,
                //not composited layers
                OSUColor backgroundColor = OpenGLPanel.Instance.BackgroundColor;
                Gl.glClearColor(backgroundColor.R, backgroundColor.G, backgroundColor.B, 1.0f);
                Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
                layer.Render();
                
                //generate unique id for the image we're going to work with
                //and make it current (this is the way DevIL works - the same way as OpenGL)
                Il.ilGenImages(1, out currentLayerName);
                Il.ilBindImage(currentLayerName);
                
                Ilut.ilutGLScreen();//save the contents of the framebuffer to the image
                //Ilut.ilutD3D8VolumeTexture
                //Il.ilCopyPixels(0, 0, 0, 100, 100, 4, Il.IL_RGBA, 
                Il.ilSave(ilFileType, filename + currentLayerNumber + "." + filetype);
                currentLayerNumber++;
                */
            }
        }
    }
}
